package com.Work.auth.service.impl;

import com.Work.auth.mapper.SysPostMapper;
import com.Work.auth.service.SysPostService;
import com.Work.model.system.SysPost;
import com.Work.vo.system.SysPostQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Override
    public Map<String, Object> pageQueryPost(Long page, Long limit, SysPostQueryVo sysPostQueryVo) {
        Page<SysPost> pageParam = new Page<>(page, limit);

        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        String postCode = sysPostQueryVo.getPostCode();
        String name = sysPostQueryVo.getName();
        Boolean status = sysPostQueryVo.getStatus();

        if (!StringUtils.isEmpty(postCode)) {
            wrapper.like(SysPost::getPostCode, postCode);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like(SysPost::getName, name);
        }
        if (status != null) {
            wrapper.eq(SysPost::getStatus, status ? 1 : 0);
        }

        IPage<SysPost> pageModel = baseMapper.selectPage(pageParam, wrapper);

        Map<String, Object> result = new HashMap<>();
        result.put("records", pageModel.getRecords());
        result.put("total", pageModel.getTotal());
        result.put("size", pageModel.getSize());
        result.put("current", pageModel.getCurrent());
        return result;
    }
}